
package com.usersmodule.servlet;
import com.usersmodule.bean.TbUsers;
import com.usersmodule.dao.*;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import util.*;

public class TbUsersServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        if("add".equals(method)){
        String userName=request.getParameter("userName");
        String userNickName=request.getParameter("userNickName");
        String userCardId=request.getParameter("userCardId");
        String userWorkId=request.getParameter("userWorkId");
        String userPass=request.getParameter("userPass");
        String userPhone=request.getParameter("userPhone");
        String userEmail=request.getParameter("userEmail");
        String userAddress=request.getParameter("userAddress");
        String userBirthday=request.getParameter("userBirthday");
        String userInCompanyTime=request.getParameter("userInCompanyTime");
        int userState=Integer.parseInt(request.getParameter("userState"));
        int parentUserId=Integer.parseInt(request.getParameter("parentUserId"));
        int createUserId=Integer.parseInt(request.getParameter("createUserId"));
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        String userMemo=request.getParameter("userMemo");
        String createTime=request.getParameter("createTime");

            if(new TbUsersDAO().add(new TbUsers(userName,userNickName,userCardId,userWorkId,userPass,userPhone,userEmail,userAddress,userBirthday,userInCompanyTime,userState,parentUserId,createUserId,isEnable,userMemo,createTime))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
             request.getSession(true).setAttribute("tbUsersList",new TbUsersDAO().getTbUsersAll());
            response.sendRedirect("UsersModule/TbUsersList.jsp");
        }
        if("del".equals(method)){
            if(UtilDAO.delin("Tb_Users", "UserId", request.getParameter("delIds"))){            
            request.getSession().setAttribute("result", "删除成功");
            }else{            
            request.getSession().setAttribute("result", "删除失败");
            }
             request.getSession(true).setAttribute("tbUsersList",new TbUsersDAO().getTbUsersAll());
            response.sendRedirect("UsersModule/TbUsersList.jsp");
        }
        if("edit".equals(method)){
        int userId=Integer.parseInt(request.getParameter("userId"));
        String userName=request.getParameter("userName");
        String userNickName=request.getParameter("userNickName");
        String userCardId=request.getParameter("userCardId");
        String userWorkId=request.getParameter("userWorkId");
        String userPass=request.getParameter("userPass");
        String userPhone=request.getParameter("userPhone");
        String userEmail=request.getParameter("userEmail");
        String userAddress=request.getParameter("userAddress");
        String userBirthday=request.getParameter("userBirthday");
        String userInCompanyTime=request.getParameter("userInCompanyTime");
        int userState=Integer.parseInt(request.getParameter("userState"));
        int parentUserId=Integer.parseInt(request.getParameter("parentUserId"));
        int createUserId=Integer.parseInt(request.getParameter("createUserId"));
        int isEnable=Integer.parseInt(request.getParameter("isEnable"));
        String userMemo=request.getParameter("userMemo");
        String createTime=request.getParameter("createTime");

            if(new TbUsersDAO().edit(new TbUsers(userId,userName,userNickName,userCardId,userWorkId,userPass,userPhone,userEmail,userAddress,userBirthday,userInCompanyTime,userState,parentUserId,createUserId,isEnable,userMemo,createTime))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
             request.getSession(true).setAttribute("tbUsersList",new TbUsersDAO().getTbUsersAll());
            response.sendRedirect("UsersModule/TbUsersList.jsp");
        }
        if ("list".equals(method)) {
        	System.out.println("list");
             request.getSession(true).setAttribute("tbUsersList",new TbUsersDAO().getTbUsersAll());
            response.sendRedirect("UsersModule/TbUsersList.jsp");
        	}
        if ("listById".equals(method)) {
             request.getSession(true).setAttribute("tbUsers",new TbUsersDAO().getTbUsersByUserId(Integer.parseInt(request.getParameter("userId"))));
            response.sendRedirect("UsersModule/TbUsersOperate.jsp");
    	}
        if("showadd".equals(method)){
            request.getSession().removeAttribute("tbUsers");
            response.sendRedirect("UsersModule/TbUsersOperate.jsp");
        }
        if("login".equals(method)){
        	TbUsers tbusers=new TbUsersDAO().checkLogin(new TbUsers(request.getParameter("username"), request.getParameter("userpass")));
        	if(tbusers!=null){
        		request.getSession().setAttribute("Users", tbusers);
        		response.sendRedirect("TbModuleServlet?method=toindex");
        	}else{
        		response.sendRedirect("login.jsp?err=1");
        	}
        }
        if("loginout".equals(method)){
        	request.getSession(true).removeAttribute("Users");
        	response.sendRedirect("login.jsp");
        }
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
